package com.fmsz.physicalheadline.fragments;


import android.databinding.DataBindingUtil;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.fmsz.physicalheadline.BR;
import com.fmsz.physicalheadline.R;
import com.fmsz.physicalheadline.adapter.news.RecycleAdapter;
import com.fmsz.physicalheadline.application.MyApplication;
import com.fmsz.physicalheadline.databinding.FragmentBaseEventBinding;
import com.fmsz.physicalheadline.databinding.PopupwindowBinding;
import com.fmsz.physicalheadline.entity.events.EventRoot;
import com.fmsz.physicalheadline.interfaces.HttpService;
import com.fmsz.physicalheadline.interfaces.SlidingOpen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseEventFragment extends Fragment implements View.OnClickListener, Callback<EventRoot> {

    private FragmentBaseEventBinding bind;
    private SlidingOpen slidingOpen;
    private PopupWindow mPopWindow;
    private PopupwindowBinding bind1;
    private RecycleAdapter<Object> adapter;
    boolean flag = true;
    private HttpService service;

public static BaseEventFragment newInstance() {
    Bundle args = new Bundle();
    BaseEventFragment fragment = new BaseEventFragment();
    fragment.setArguments(args);
    return fragment;
}
    public BaseEventFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        slidingOpen = (SlidingOpen) getActivity();
        View view = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_base_event, container, false).getRoot();
        bind = DataBindingUtil.bind(view);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        bind.imageTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingOpen.isOpenOrClose();
            }
        });
        bind.linearLayoutHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag) {
                    showPopupWindow();
                    flag = false;
                    Animation animation = new RotateAnimation(0, 180, bind.moreEvent.getWidth() / 2, bind.moreEvent.getHeight() / 2);
                    animation.setDuration(500);
                    animation.setFillAfter(true);
                    bind.moreEvent.startAnimation(animation);

                } else {
                    mPopWindow.dismiss();
                    flag = true;
                    Animation animation = new RotateAnimation(180, 360, bind.moreEvent.getWidth() / 2, bind.moreEvent.getHeight() / 2);
                    animation.setDuration(500);
                    animation.setFillAfter(true);
                    bind.moreEvent.startAnimation(animation);
                }
            }
        });


        Map<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(null, Pair.create(R.layout.events_item, BR.con_list_e));
        adapter = new RecycleAdapter<>(getContext(), new ArrayList<>(), map);
        bind.recycleViewEvents.setAdapter(adapter);
        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDrawOver(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0, 0, 0, 3);
            }
        };
        bind.recycleViewEvents.addItemDecoration(decoration);

        service = ((MyApplication) getContext().getApplicationContext()).getHttpService();
        service.getAllMatch().enqueue(this);

    }

    private void showPopupWindow() {
        View view = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.popupwindow, null, false).getRoot();
        bind1 =  DataBindingUtil.bind(view);
        mPopWindow = new PopupWindow(view);
        mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.showAsDropDown(this.bind.linearLayoutHead);

        bind1.recommend.setOnClickListener(this);
        bind1.all.setOnClickListener(this);
        bind1.nba.setOnClickListener(this);
        bind1.csoccer.setOnClickListener(this);
        bind1.isoccer.setOnClickListener(this);
        bind1.cbasketball.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recommend:
                service.getHotMatch().enqueue(this);
                mPopWindow.dismiss();
                break;
            case R.id.all:
                service.getAllMatch().enqueue(this);
                mPopWindow.dismiss();
                break;
            case R.id.nba:
                service.getNBAMatch().enqueue(this);
                mPopWindow.dismiss();
                break;
            case R.id.csoccer:
                service.getCFMatch().enqueue(this);
                mPopWindow.dismiss();
                break;
            case R.id.isoccer:
                service.getIFMatch().enqueue(this);
                mPopWindow.dismiss();
                break;
            case R.id.cbasketball:
                service.getCBMatch().enqueue(this);
                mPopWindow.dismiss();
                break;
        }

        Animation animation = new RotateAnimation(180, 360, bind.moreEvent.getWidth() / 2, bind.moreEvent.getHeight() / 2);
        animation.setDuration(500);
        animation.setFillAfter(true);
        bind.moreEvent.startAnimation(animation);
    }

    @Override
    public void onResponse(Call<EventRoot> call, Response<EventRoot> response) {
        EventRoot body = response.body();
        adapter.removeAll();
        adapter.addAll(body.getData().getContest_list());


    }

    @Override
    public void onFailure(Call<EventRoot> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        slidingOpen = null;
    }
    
}
